﻿using DeZhen.DataCollection.IService.CollectionTemplate;
using DeZhen.DataCollection.Model.DTO.CollectionTemplate;
using DeZhen.DataCollection.Model.Enums;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace DeZhen.DataCollection.WebApi.Controllers
{
    /// <summary>
    /// 采集业务模版设置
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CollectionTemplateController : ControllerBase
    {
        private readonly ICollectionTemplateService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        public CollectionTemplateController(ICollectionTemplateService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取采集业务模版下拉列表
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "GetTemplateDropDown")]
        public async Task<List<CollectionTemplateDropDownDTO>> GetTemplateDropDownAsync()
        {
            return await _service.GetDropDownListAsync();
        }

        /// <summary>
        /// 获取采集业务模板参数设置列表
        /// </summary>
        /// <param name="collectionType">采集类型：1.按患者采集，2.按医务人员采集</param>
        /// <param name="templateId">采集业务模板ID</param>
        /// <returns></returns>
        [HttpGet(Name = "GetTemplateParamSetting")]
        public async Task<List<TemplateParamSettingDTO>> GetTemplateParamSettingAsync([Required] CollectionTypeEnum collectionType, long? templateId)
        {
            return await _service.GetTemplateParamSettingAsync(collectionType, templateId);
        }

        /// <summary>
        /// 保存采集业务模板参数设置
        /// </summary>
        /// <param name="input">采集业务模板输入参数</param>
        /// <returns></returns>
        [HttpPost(Name = "SaveTemplateParamSetting")]
        public async Task<bool> SaveTemplateParamSettingAsync(SaveTemplateParamSetting input)
        {
            return await _service.SaveTemplateParamSettingAsync(input);
        }

        /// <summary>
        /// 检查采集业务模板参数设置是否存在
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet(Name = "ExistsParamSetting")]
        public async Task<bool> ExistsTemplateParamSettingAsync([FromQuery] QueryTemplateParamSetting param)
        {
            return await _service.ExistsTemplateParamSetting(param);
        }

        /// <summary>
        /// 获取采集业务模板预览SQL列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet(Name = "GetPreviewSqlList")]
        public async Task<List<PreviewSqlDTO>> GetPreviewSqlListAsync([FromQuery] QueryTemplateParamSetting param)
        {
            return await _service.GetPreviewSqlListAsync(param);
        }

        /// <summary>
        /// 保存采集业务模板
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost(Name = "SvaeCollectionTemplate")]
        public async Task<bool> SaveCollectionTemplateAsync(CollectionTemplateAdd input)
        {
            return await _service.SaveCollectionTemplateAsync(input);
        }

        /// <summary>
        /// 删除采集业务模板
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        [HttpDelete(Name = "DeleteCollectionTemplate")]
        public async Task<bool> DeleteCollectionTemplateAsync([Required] long templateId)
        {
            if (templateId <= 0)
            {
                throw new ArgumentNullException(nameof(templateId), "模板ID不能为空");
            }
            return await _service.DeleteCollectionTemplateAsync(templateId);
        }
    }
}
